package com.jy.unkown;

import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.ui.PlayerView;
import com.jy.unkown.entity.UnkownAdBean;
import com.jy.unkown.view.UnkownADView;

import java.util.HashSet;
import java.util.List;

public class UnkownVideoPoint2 implements PlayProgressListener, Runnable {

    List<UnkownAdBean.TracksDTO> tracks;
    PlayerView unkowVideoView;
    UnkownADView unkownADView;
    HashSet<Integer> hashSet = new HashSet<>();
    boolean isStart = true;

    public UnkownVideoPoint2(UnkownADView unkownADView, PlayerView unkowVideoView, List<UnkownAdBean.TracksDTO> list) {
        this.unkowVideoView = unkowVideoView;
        this.unkownADView = unkownADView;
        this.tracks = list;
    }

    public void setStart(boolean start) {
        isStart = start;
    }

    public void init() {
        new Thread(this).start();
    }

    /**
     * 播放进度
     * 0—播放进度 0%(开始播放)
     * 1—播放进度 25%
     * 2—播放进度 50%
     * 3—播放进度 75%
     * 4—播放进度 100%(播放结束)
     * 101-视频全屏播放
     * 102-退出全屏播放
     * 103-静音播放
     * 104-关闭静音
     * 105-播放视频时有屏幕上滑操作
     * 106-播放视频时有屏幕下滑操作
     * 107-视频暂停播放
     * 108-视频暂停后继续播放
     * 109-播放结束后重新播放
     *
     * @param curr
     * @param total
     */

    boolean isEnd;
    boolean isPosted = false;
    HashSet<String> pintHashSet = new HashSet<>();

    @Override
    public void play(int curr, int total, int progress) {
        if (!isEnd) {
            isEnd = progress == 100;
        }

//        if (progress >= 95  ) {
//            isStart = false;
//            String key = "播放进度100";
//            if (pintHashSet.contains(key)) {
//                return;
//            }
//            pintHashSet.add(key);
//
//            unkownADView.pointPost(UnkownADView.PLAY_VIDEO, key, null);
//
//        } else if (progress >= 70 && progress <= 80  ) {
//            String key = "播放进度75";
//            if (pintHashSet.contains(key)) {
//                return;
//            }
//            pintHashSet.add(key);
//
//            unkownADView.pointPost(UnkownADView.PLAY_VIDEO, key, null);
//
//
//        } else if (progress >= 45 && progress <= 55 ) {
//            String key = "播放进度50";
//            if (pintHashSet.contains(key)) {
//                return;
//            }
//            pintHashSet.add(key);
//
//            unkownADView.pointPost(UnkownADView.PLAY_VIDEO, key, null);
//
//
//        } else if (progress >= 20 && progress <= 30  ) {
//            String key = "播放进度25";
//            if (pintHashSet.contains(key)) {
//                return;
//            }
//            pintHashSet.add(key);
//
//            unkownADView.pointPost(UnkownADView.PLAY_VIDEO, key, null);
//        } else if (progress >= 0 && progress <= 10  ) {
//            String key = "播放进度0";
//            if (pintHashSet.contains(key)) {
//                return;
//            }
//            pintHashSet.add(key);
//
//            unkownADView.pointPost(UnkownADView.PLAY_VIDEO, key, null);
//        }



        if (tracks != null) {
            for (int i = 0; i < tracks.size(); i++) {
                UnkownAdBean.TracksDTO tracksDTO = tracks.get(i);
                if (tracksDTO != null) {
                    if (progress >= 95 && tracksDTO.type == 4) {
                        isStart = false;
                        String key = "播放进度100";
                        if (pintHashSet.contains(key)) {
                            return;
                        }
                        pintHashSet.add(key);

                        unkownADView.pointPost(UnkownADView.PLAY_VIDEO, key, tracksDTO.urls);

                    } else if (progress >= 70 && progress <= 80 && tracksDTO.type == 3) {
                        String key = "播放进度75";
                        if (pintHashSet.contains(key)) {
                            return;
                        }
                        pintHashSet.add(key);

                        unkownADView.pointPost(UnkownADView.PLAY_VIDEO, key, tracksDTO.urls);


                    } else if (progress >= 45 && progress <= 55 && tracksDTO.type == 2) {
                        String key = "播放进度50";
                        if (pintHashSet.contains(key)) {
                            return;
                        }
                        pintHashSet.add(key);

                        unkownADView.pointPost(UnkownADView.PLAY_VIDEO, key, tracksDTO.urls);


                    } else if (progress >= 20 && progress <= 30 && tracksDTO.type == 1) {
                        String key = "播放进度25";
                        if (pintHashSet.contains(key)) {
                            return;
                        }
                        pintHashSet.add(key);

                        unkownADView.pointPost(UnkownADView.PLAY_VIDEO, key, tracksDTO.urls);
                    } else if (progress >= 0 && progress <= 10 && tracksDTO.type == 0) {
                        String key = "播放进度0";
                        if (pintHashSet.contains(key)) {
                            return;
                        }
                        pintHashSet.add(key);

                        unkownADView.pointPost(UnkownADView.PLAY_VIDEO, key, tracksDTO.urls);
                    }


                    if (isEnd && !isPosted && progress > 0) {
                        isPosted = true;
                        if (tracksDTO.type == 109) {
                            isStart = false;
                            unkownADView.pointPost(UnkownADView.PLAY_VIDEO, "播放结束后重新播放 ", tracksDTO.urls);
                        }
                    }
                }
            }
        }
    }

    String ss = "213";

    @Override
    public void run() {
        try {
            Thread.sleep(2000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        while (isStart) {
            synchronized (ss) {
                try {
                    try {
                        ss.wait(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Player player = unkowVideoView.getPlayer();
                    if (player.isPlaying()){
                        long curr = player.getCurrentPosition();
                        long total = player.getDuration();
                        if (total != 0) {
                            play((int) curr, (int) total, (int) (curr * 100 / total));
                        }
                    }
                }catch (Exception exception){

                }
            }
        }
    }
}
